//
//  UIView+XMBorder.m
//  XMKit
//
//  Created by chenming on 2017/12/21.
//  Copyright © 2017年 ChenXiaoMing. All rights reserved.
//

#import "UIView+XMBorder.h"

@implementation UIView (XMBorder)

- (UIView *)xm_borderForColor:(UIColor *)color borderWidth:(CGFloat)borderWidth borderStyle:(XMViewBorderStyle)borderStyle {
    switch (borderStyle) {
        case XMViewBorderStyleAll:
            {
                self.layer.borderWidth = borderWidth;
                self.layer.borderColor = color.CGColor;
            }
            break;
        case XMViewBorderStyleLeft:
        {
            [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.f, 0.f) toPoint:CGPointMake(0.0f, self.frame.size.height) color:color borderWidth:borderWidth]];
        }
            break;
        case XMViewBorderStyleRight:
        {
            [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(self.frame.size.width, 0.0f) toPoint:CGPointMake(self.frame.size.width, self.frame.size.height) color:color borderWidth:borderWidth]];
        }
            break;
        case XMViewBorderStyleTop:
        {
            [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, 0.0f) toPoint:CGPointMake(self.frame.size.width, 0.0f) color:color borderWidth:borderWidth]];
        }
            break;
        case XMViewBorderStyleBottom:
        {
            [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, self.frame.size.height) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:color borderWidth:borderWidth]];
        }
            break;
        default:
            break;
    }
    return self;
}

- (void)xm_logView:(UIView *)view {
    XMLog(@"<- ! -> = %@",[view description]);
    for (UIView *subView in view.subviews) {
        [self xm_logView:subView];
    }
}

- (CAShapeLayer *)addLineOriginPoint:(CGPoint)p0 toPoint:(CGPoint)p1 color:(UIColor *)color borderWidth:(CGFloat)borderWidth {
    // 线的路径
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:p0];
    [bezierPath addLineToPoint:p1];
    
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = color.CGColor;
    shapeLayer.fillColor  = [UIColor clearColor].CGColor;
    // 添加路径
    shapeLayer.path = bezierPath.CGPath;
    // 线宽度
    shapeLayer.lineWidth = borderWidth;
    return shapeLayer;
}

@end
